package com.dqhl.community.activity;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.chanven.lib.cptr.PtrClassicFrameLayout;
import com.chanven.lib.cptr.PtrDefaultHandler;
import com.chanven.lib.cptr.PtrFrameLayout;
import com.chanven.lib.cptr.loadmore.OnLoadMoreListener;
import com.dqhl.community.R;
import com.dqhl.community.adapter.PayRecordListAdapter;
import com.dqhl.community.base.BaseActivity;
import com.dqhl.community.model.PayRecord;
import com.dqhl.community.util.Config;
import com.dqhl.community.util.Constant;
import com.dqhl.community.util.Dlog;
import com.dqhl.community.util.JsonUtils;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by guanlucoang on 2016/8/30.
 * 物业缴费记录 activity
 */
public class PayRecordActivity extends BaseActivity implements View.OnClickListener {

    private TextView tv_topTitle;
    private ImageView iv_topBack;

    private ListView lvPayRecord;
    private PayRecordListAdapter payRecordListAdapter;
    private List<PayRecord> payRecordList = new ArrayList<>();
    private PtrClassicFrameLayout ptrClassicFrameLayout;
    Handler handler = new Handler();
    private TextView tv_no_info;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pay_record);
        // 实例化对象
        initView();
        // 实例化数据
        initData();
    }

    // 实例化对象
    private void initView() {
        tv_topTitle = (TextView) findViewById(R.id.tv_top_title);
        iv_topBack = (ImageView) findViewById(R.id.iv_top_back);
        lvPayRecord = (ListView) findViewById(R.id.lv_pay_record);
        ptrClassicFrameLayout = (PtrClassicFrameLayout) findViewById(R.id.ptr_list);
        tv_no_info = (TextView) findViewById(R.id.tv_no_info);

        tv_topTitle.setText(R.string.propertyPay);
        iv_topBack.setVisibility(View.VISIBLE);
        iv_topBack.setOnClickListener(this);
        //下拉刷新
        ptrClassicFrameLayout.postDelayed(new Runnable() {

            @Override
            public void run() {
                ptrClassicFrameLayout.autoRefresh(true);
            }
        }, 150);

        ptrClassicFrameLayout.setPtrHandler(new PtrDefaultHandler() {

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        ptrClassicFrameLayout.refreshComplete();
                        //获取数据
                        initData();
                    }
                }, 1000);
            }
        });

        ptrClassicFrameLayout.setOnLoadMoreListener(new OnLoadMoreListener() {

            @Override
            public void loadMore() {
                handler.postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        ptrClassicFrameLayout.loadMoreComplete(true);

                    }
                }, 1000);
            }
        });
    }

    /**
     * 实例化数据
     */
    private void initData() {
        showCustomLoadBar("加载中");
        RequestParams params = new RequestParams(Config.Api.chargList);
        params.addBodyParameter("userid", spUserInfo.getString(Constant.USERID, ""));
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Dlog.e(result);
                int errCode = JsonUtils.getErrCode(result);
                String data = JsonUtils.getInfo(result, "data");
                if (errCode == 0) {
                    payRecordList = JSON.parseArray(data, PayRecord.class);
                    if (payRecordList.size() > 0) {
                        for (int i = 0; i < payRecordList.size(); i++) {
                            payRecordList.get(i).setOwner_name(spUserInfo.getString(Constant.NAME, ""));
                        }
                        payRecordListAdapter = new PayRecordListAdapter(PayRecordActivity.this, payRecordList);
                        lvPayRecord.setAdapter(payRecordListAdapter);
                    } else {
                        ptrClassicFrameLayout.setVisibility(View.GONE);
                        tv_no_info.setVisibility(View.VISIBLE);
                    }
                } else {
                    ptrClassicFrameLayout.setVisibility(View.GONE);
                    tv_no_info.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                ptrClassicFrameLayout.setVisibility(View.GONE);
                tv_no_info.setVisibility(View.VISIBLE);
                toast(R.string.error_net_connection);
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {
                hideCustomLoadBar();
            }
        });

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_top_back:
                finish();
                break;
        }
    }

}
